package com.victor9ac.mashup.elasticsearch.converter;

import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;

import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.common.xcontent.XContentBuilder;

import com.victor9ac.mashup.api.Comment;
import com.victor9ac.mashup.api.Commentaries;
import com.victor9ac.mashup.api.Image;
import com.victor9ac.mashup.api.Images;

public class ElasticSearchImagesConverter implements ElasticSearchConverter {

	public Object convert(SearchResponse searchResponse) throws ParseException {
		// TODO Auto-generated method stub
		return null;
	}

	public List<XContentBuilder> buildJson(Object obj) throws IOException {
		Images images = (Images) obj;

		List<XContentBuilder> contentList = new ArrayList<XContentBuilder>();

		for (Image comment : images.getImgs()) {
			contentList.add(buildContent(comment, images.getKeywordLink()));
		}

		return contentList;
	}

	XContentBuilder buildContent(Image img, String keyword)
			throws IOException {
		XContentBuilder json = jsonBuilder().startObject()
				.field("keyword", keyword.toUpperCase())
				.field("tittle", img.getTittle())
				.field("description", img.getDescription())
				.field("imageUrl", img.getUrl())
				.field("dataDate", new Date())
				.endObject();
		return json;
	}

}
